package cn.interestingshop.service;

import cn.interestingshop.dao.classify.ClassifyDao;
import cn.interestingshop.dao.classify.ClassifyDaoImpl;
import cn.interestingshop.model.entity.Classify;
import cn.interestingshop.model.entity.VO.ClassifyVo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/3/10 14:25
 **/
public class ClassifyServiceImpl implements  ClassifyService {
	ClassifyDao classifyDao = new ClassifyDaoImpl();
	@Override
	public List<ClassifyVo> findAll() {
		List<ClassifyVo> first = new ArrayList<ClassifyVo>(); //一级
		try {
			List<Classify> lv1 = classifyDao.findClassifyByParentId(null);
			for (Classify tmp : lv1){
				List<ClassifyVo> second = new ArrayList<ClassifyVo>(); //二级

				ClassifyVo vo1 = new ClassifyVo();
				vo1.setClassify(tmp);
				vo1.setNext(second);//添加二级
				first.add(vo1);

				List<Classify> lv2 = classifyDao.findClassifyByParentId(tmp.getId());
				for (Classify tmp2 : lv2){
					List<ClassifyVo> third = new ArrayList<ClassifyVo>(); //三级
					ClassifyVo vo2 = new ClassifyVo();
					vo2.setClassify(tmp2);
					vo2.setNext(third);//添加三级
					second.add(vo2);

					List<Classify> lv3 = classifyDao.findClassifyByParentId(tmp2.getId());
					for (Classify tmp3 : lv3){
						ClassifyVo vo3 = new ClassifyVo();
						vo3.setClassify(tmp3);
						third.add(vo3);
					}

				}
			}
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		return first;
	}

	@Override
	public List<Classify> findClassifyByParentId(Integer parentId) {
		try {
			return classifyDao.findClassifyByParentId(parentId);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		return null;
	}
}
